﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game2 : MonoBehaviour 
{

    public TanTaShuiNiBan shuiNiBan;
    public CameraPathBezierAnimator cameraPathAni;
    public GameObject curser;
    public GameObject cameraRig;
    public GameObject dimian1;
    private bool isStartRotate = false;
    public MyCameraEffect myCameraEffect;
    public EndTip endTip;
    public AudioClip clip;

    public InputController leftController;
    public InputController rightController;
    void Awake()
    {
        PlayerPrefs.SetString("sceneName", "Scene2");
    }
    void Start()
    {
        SoundManager.instance.PlayBGMusic(clip);
        StartPlay();
        curser.GetComponent<DistanceEvent>().OnNearEvent.AddListener(GameStart);

        shuiNiBan.onPlayAnimation += ShuiNiBan_onPlayAnimation;

        myCameraEffect.OnCancleBlack += MyCameraEffect_OnCancleBlack;
        TriggerEvent triggerEvent= cameraRig.GetComponent<TriggerEvent>();
        triggerEvent.targertEntered = dimian1;
        triggerEvent.onOtherEnterEvent.AddListener(PlayLuoDiSound);
        BoxCollider box= cameraRig.AddComponent<BoxCollider>();
        box.size = new Vector3(1.3f, 2f, 1.3f);
        box.center = new Vector3(0,1.9f,0);
        Debug.Log(dimian1.name);
    }

    private void GameStart()
    {
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
        shuiNiBan.PlayAnimation();
        curser.SetActive(false);
    }

    private void MyCameraEffect_OnCancleBlack()
    {
        endTip.ShowTip(TipWords.game2_tanta);
    }

    void Update()
    {
        if (isStartRotate == true)
        {
            cameraRig.transform.Rotate(Vector3.right, 2f);
        }
    }

    /// <summary>
    /// 摄像机旋转
    /// </summary>
    void CameraRotate(bool isRotate)
    {
        Debug.Log("摄像机开始旋转");
        isStartRotate = isRotate;
    }

    private void ShuiNiBan_onPlayAnimation()
    {
        Invoke("PlayScreamSound",0.2f);
    }

    /// <summary>
    /// 两种模式切换
    /// </summary>
    private void StartPlay()
    {
        if (PlayerPrefs.GetString("mode") == "教育模式")
        {
            Debug.Log("开始教育模式");
            Debug.Log("当前场景为：" + PlayerPrefs.GetString("sceneName"));
            cameraPathAni.Play();
            MyControllerManager.instance.DisableController();
        }
        else
        {
            Debug.Log("开始体验模式");
            Debug.Log("当前场景为：" + PlayerPrefs.GetString("sceneName"));
        }
    }

    public void PlayScreamSound()
    {
        print("尖叫");
        SoundManager.instance.Play("jianjiaosheng_Femal");
        CameraRotate(true);
        Invoke("DelayFallDown", 0.5f);
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
    }




    void DelayFallDown()
    {
        Rigidbody rig = cameraRig.AddComponent<Rigidbody>();
        
        rig.useGravity = true;
        if (rig != null)
        {
            rig.drag = 0.2f;
            rig.collisionDetectionMode = CollisionDetectionMode.Continuous;
        }

    }

    private void PlayLuoDiSound()
    {
        Debug.Log(1111111);
        isStartRotate = false;
        SoundManager.instance.Play("luodigangguan");
        myCameraEffect.PlayDeadBlood();
        MyControllerManager.instance.EnableController();
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
    }
}
